package com.xingda.packing.dmss.minitor.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xingda.packing.dmss.annotation.LimitRequest;
import com.xingda.packing.dmss.common.OperationConstant;
import com.xingda.packing.dmss.common.R;
import com.xingda.packing.dmss.config.ThreadLocalConfig;
import com.xingda.packing.dmss.domain.ShuntProductionLine;
import com.xingda.packing.dmss.enums.WorkAreaType;
import com.xingda.packing.dmss.annotation.OpLog;
import com.xingda.packing.dmss.minitor.dto.CleanShuntDto;
import com.xingda.packing.dmss.minitor.dto.CleanShuntWheelDto;
import com.xingda.packing.dmss.minitor.dto.UserInfo;
import com.xingda.packing.dmss.minitor.entity.PkShuntArea;
import com.xingda.packing.dmss.minitor.entity.PkShuntWheel;
import com.xingda.packing.dmss.minitor.mapper.PkShuntAreaMapper;
import com.xingda.packing.dmss.minitor.mapper.PkShuntWheelMapper;
import com.xingda.packing.dmss.minitor.service.IpkLogService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/shunt")
public class ShuntAreaController {
    @Resource
    private ShuntProductionLine shuntProductionLine;
    @Resource
    private IpkLogService ipkLogService;
    @Resource
    private ThreadLocalConfig threadLocalConfig;
    @Resource
    private PkShuntAreaMapper pkShuntAreaMapper;
    @Resource
    private PkShuntWheelMapper pkShuntWheelMapper;

    @GetMapping("/show/{area}")
    public R showInfo(@PathVariable int area) {
        List<PkShuntWheel> pkWheels = new ArrayList<>();
        LambdaQueryWrapper<PkShuntArea> pkShuntAreaLambdaQueryWrapper = new LambdaQueryWrapper<>();
        pkShuntAreaLambdaQueryWrapper.eq(PkShuntArea::getArea, area);
        pkShuntAreaLambdaQueryWrapper.eq(PkShuntArea::getTag, 1);
        List<PkShuntArea> pkShuntAreas = pkShuntAreaMapper.selectList(pkShuntAreaLambdaQueryWrapper);
        if (!pkShuntAreas.isEmpty()) {
            List<Integer> collect = pkShuntAreas.stream().map(PkShuntArea::getWheelId).collect(Collectors.toList());
            LambdaQueryWrapper<PkShuntWheel> pkShuntWheelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            pkShuntWheelLambdaQueryWrapper.in(PkShuntWheel::getId, collect);
            pkWheels = pkShuntWheelMapper.selectList(pkShuntWheelLambdaQueryWrapper);
        }
        return R.ok().put("data", pkWheels);
    }

    @LimitRequest(count = 1)
    @OpLog(username = "#cleanShuntDto.username",
            corp = "#cleanShuntDto.corp",
            veri = "#cleanShuntDto.veri",
            opItem = OperationConstant.REMOVE_ALL_SHUNTAREA_WHEEL,
            param = "#cleanShuntDto.area")
    @PostMapping("/cleanShuntArea")
    public R cleanShuntArea(@RequestBody CleanShuntDto cleanShuntDto) {
        try {
            shuntProductionLine.getShuntArea().cleanAllToSql(cleanShuntDto.getArea());
            UserInfo userInfo = threadLocalConfig.get();
            userInfo.setOperation(OperationConstant.REMOVE_ALL_SHUNTAREA_WHEEL + ",区域" + WorkAreaType.getByID(cleanShuntDto.getArea()).getTypeName());
            ipkLogService.saveLog(userInfo);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    @LimitRequest(count = 1)
    @OpLog(username = "#cleanShuntWheelDto.username",
            corp = "#cleanShuntWheelDto.corp",
            veri = "#cleanShuntWheelDto.veri",
            opItem = OperationConstant.REMOVE_SHUNT_WHEEL,
            param = "#cleanShuntWheelDto.area")
    @PostMapping("/cleanShuntWheel")
    public R cleanShuntWheel(@RequestBody CleanShuntWheelDto cleanShuntWheelDto) {
        try {
            LambdaQueryWrapper<PkShuntArea> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(PkShuntArea::getArea, cleanShuntWheelDto.getArea());
            lambdaQueryWrapper.eq(PkShuntArea::getBarCode, cleanShuntWheelDto.getBarCode());
            PkShuntArea pkShuntArea = pkShuntAreaMapper.selectOne(lambdaQueryWrapper);
            pkShuntArea.setTag(0);
            pkShuntAreaMapper.updateById(pkShuntArea);
            UserInfo userInfo = threadLocalConfig.get();
            userInfo.setOperation(OperationConstant.REMOVE_SHUNT_WHEEL + ",区域" + WorkAreaType.getByID(cleanShuntWheelDto.getArea()).getTypeName());
            ipkLogService.saveLog(userInfo);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }
}
